﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.DataAccess.Configurations
{
    public class CategoryConfig : EntityTypeConfiguration<Category>
    {
        public CategoryConfig()
        {
            #region Relaionships

            HasOptional(p => p.Parent).WithMany(p => p.ChildCategories).HasForeignKey(p => p.ParentId).WillCascadeOnDelete(false);
          //HasMany(p => p.ChildCategories).WithOptional(p => p.Parent).HasForeignKey(p => p.ParentId).WillCascadeOnDelete(false);

            HasMany(p => p.Items).WithMany(p => p.Categories);
            
            #endregion

            #region Properties

            HasKey(p => p.Id);

            Property(p => p.Id)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(32)
                    .IsRequired();

            Property(p => p.IsVisible)
                    .HasColumnType("bit")
                    .IsRequired();

            Property(p => p.Title)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsRequired();                  // "Optional" in original, but a category with no content doesn't make sense, so it's changed to "Required"

            Property(p => p.Desc)
                    .HasColumnName("Description")
                    .HasColumnType("nvarchar")
                    .HasMaxLength(2048)
                    .IsOptional();

            Property(p => p.ImageUrl)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsOptional();

            Property(p => p.ImageAltText)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(256)
                    .IsOptional();

            Property(p => p.ParentId)
                    .HasColumnType("nvarchar")
                    .HasMaxLength(32)
                    .IsOptional();

            // Property(p => p.DisplayOrder)        // defined in the original kit but never used 
            //        .HasColumnType("int")
            //        .IsOptional();

            #endregion
        }
    }
}
